<?php

namespace App\Http\Middleware;

use Closure;
//use Example\Models\OperationLog as Model;

class OperationLog
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (request()->method() !== 'GET') {
            $actions = [
                'switch' => '开关',
                'store' => '创建',
                'remove' => '删除',
                'edit' => '更新',
                'import' => '导入',
                'approved' => '审核通过',
                'rejected' => '审核驳回',
            ];
            $pathArray = explode('/', request()->path());
            $action =  $actions[end($pathArray)] ?? '';
//            dd($pathArray);
            $data = [
                'path' => $request->path(),
                'params' => $request->all(),
                'action' => $action,
                'operation_ip' => getRealIp($request->ip()),
                'operation_id' => auth()->user()->id ?? 0,
            ];
//            dd($data);
            //Model::create($data);
        }

        return $next($request);
    }
}
